package org.freedom.fastdevpt.gos.company.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;

import org.freedom.fastdevpt.core.msg.OptMsg;
import org.freedom.fastdevpt.gos.company.pojo.Company;
import org.freedom.fastdevpt.gos.company.pojo.CompanyQuery;
import org.freedom.fastdevpt.gos.company.service.CompanyService;
import org.freedom.fastdevpt.utils.GosDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller()
@RequestMapping("/gos/company")
public class CompanyController {
	public static ResourceBundle rs = ResourceBundle.getBundle("i18n.gos.company.msg");
	@Autowired
	private CompanyService companyService;

	@RequestMapping("/index")
	public String index() {

		return "gos/company/index";
	}

	@RequestMapping("/add")
	public String add() {

		return "gos/company/add";
	}

	@RequestMapping("/ins")
	@ResponseBody
	public Object save(Company company) {
		OptMsg msg;
		try {
			company.setCreatetime(GosDateUtils.now());
			companyService.save(company);
			msg = new OptMsg(true, rs.getString("success"), "info", rs.getString("title.add"));
		} catch (Exception e) {
			msg = new OptMsg(false, e.getMessage(), "error", rs.getString("title.add"));

		}
		return msg;
	}

	@RequestMapping("/edit")
	public String edit(Integer pk, HttpServletRequest request) {
		Company detail = companyService.getByPk(pk);
		request.setAttribute("dt", detail);
		return "gos/company/edit";
	}

	@RequestMapping("/upd")
	@ResponseBody
	public Object update(Company company) {
		OptMsg msg;
		try {
			boolean result = companyService.update(company);
			msg = new OptMsg(result, rs.getString("success"), "info", rs.getString("title.edit"));
		} catch (Exception e) {
			msg = new OptMsg(false, e.getMessage(), "error", rs.getString("title.edit"));

		}
		return msg;
	}

	@RequestMapping("/dt")
	public String detail(Integer pk, HttpServletRequest request) {
		Company detail = companyService.getByPk(pk);
		request.setAttribute("dt", detail);
		return "gos/company/dt";
	}

	@RequestMapping("/list")
	@ResponseBody
	public Object list(CompanyQuery q) {
		List<Company> list = companyService.getByQuery(q);
		Long count = companyService.getCountByQuery(q);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", count);
		result.put("rows", list);
		return result;
	}

	@RequestMapping("/del")
	@ResponseBody
	public Object del(@RequestParam(value = "pk[]") Integer[] pk) {
		OptMsg msg;
		try {
			boolean result = companyService.batchdelete(pk);
			msg = new OptMsg(result, rs.getString("success"), "info", rs.getString("title.del"));
		} catch (Exception e) {
			msg = new OptMsg(false, e.getMessage(), "error", rs.getString("title.del"));

		}
		return msg;
	}

}
